#ifndef REGISTRATION_LIB_H_
#define REGISTRATION_LIB_H_

#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <iomanip>

#include "Registration.h"
#include "Student.h"
#include "Student_lib.h"
#include "Course.h"
#include "Course_lib.h"
using namespace std;

void Add_registration(string s, Registration l[], int &numberRegistration){
    string temp, a_sid;
    int a_lid, a_stid;

    int mark = s.find(":");
    a_stid = atoi(s.substr(0,mark).c_str());

    temp = s.substr(mark+1);
    mark = temp.find(":");
    a_sid = temp.substr(0,mark);

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_lid = atoi(temp.substr(0,mark).c_str());

    Registration Reg(a_stid, a_sid, a_lid);
    l[numberRegistration] = Reg;
    numberRegistration++;
}

void Read_registration(string _file, Registration l[], int &numberRegistration){
    ifstream fin;
    fin.open(_file.c_str());
    if(!fin.is_open()){
        cout<<"Cannot open file.\n";
        exit(1);
    }else{
        string s = "";
        while (getline(fin, s)){
            Add_registration(s, l, numberRegistration);
        }
        fin.close();
    }
}

void Remove_registrations(Registration r[], int numberRegistrationRemove, Registration l[], int &numberRegistration){
    string t_sid, t_sid_r;
    int i = 0, j = 0, k, t_lid, t_lid_r, t_stu_id, t_stu_id_r;

    for (i = 0; i < numberRegistrationRemove; i++){
        t_sid_r = r[i].Get_sid();
        t_lid_r = r[i].Get_lid();
        t_stu_id_r = r[i].Get_stu_id();

        for (j = 0; j < numberRegistration; j++){
            t_sid = l[j].Get_sid();
            t_lid = l[j].Get_lid();
            t_stu_id = l[j].Get_stu_id();

            if ((t_sid == t_sid_r) && (t_lid == t_lid_r) && (t_stu_id == t_stu_id_r)){
                for (k = j; k < numberRegistration; k++){
                    l[k] = l[k+1];
                }

                numberRegistration--;
            }
        }
    }
}

void Get_students_by_course(string sid, int lid, Registration l[], int numberRegistration, Student result[], int &numberStudentResult){
    string t_sid;
    int i = 0, j = 0, t_lid, t_stu_id;
    Student stu[100];

    Read_student("Student.txt", stu, i);

    numberStudentResult = 0;

    for (j = 0; j < numberRegistration; j++){
        t_sid = l[j].Get_sid();
        t_lid = l[j].Get_lid();

        if ((t_sid == sid) && (t_lid == lid)){
            t_stu_id = l[j].Get_stu_id();
            result[numberStudentResult] = Get_student_by_id(t_stu_id, stu, i);
            numberStudentResult++;
        }
    }
}

void Get_courses_by_student(int stu_id, Registration l[], int numberRegistration, Course result[], int &numberCourseResult){
    string t_sid;
    int i = 0, j = 0, k = 0, m = 0, t_lid, t_stu_id;
    Course c[100], t_c[100];

    Read_course("Course.txt", c, i);

    numberCourseResult = 0;

    for (j = 0; j < numberRegistration; j++){
        t_stu_id = l[j].Get_stu_id();

        if (t_stu_id == stu_id){
            t_sid = l[j].Get_sid();
            t_lid = l[j].Get_lid();
            Get_courses_by_sid_lid(t_sid, t_lid, c, i, t_c, k);

            for (m = 0; m < k; m++){
                result[numberCourseResult] = t_c[m];
                numberCourseResult++;
            }

            k = 0;
        }
    }
}

void Get_courses_by_lecturer(int lid, Registration l[], int numberRegistration, Course result[], int &numberCourseResult){
    int i, j, t_lid;
    Course c[100];

    i = 0;
    Read_course("Course.txt", c, i);

    for (j = 0; j < i; j++){
        t_lid = c[j].Get_lid();

        if (t_lid == lid){
            result[numberCourseResult] = c[j];
            numberCourseResult++;
        }
    }
}

string Get_students_list(Student stu[], int numberStudent){
    string _result = "";
    int i = 0;

    for (i = 0; i < numberStudent; i++){
        ostringstream ss;
        ss <<setw(3) << stu[i].Get_id() << setw(25) << stu[i].Get_fname() << setw(15) << stu[i].Get_dob() << setw(6) << stu[i].Get_year()<< "\n";
        _result = _result + ss.str();
    }

    return _result;
}
#endif // REGISTRATION_LIB_H_
